<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class client extends CI_Controller {

	function __construct()
	{
		parent::__construct(); 
		$this->load->model('api/m_client');
		parse_str($_SERVER['QUERY_STRING'], $_GET); //解析链接，直接放到GET中
	}	

	
	
/*
|--------------------------------------------------------------------------
| 验证账号，返回token
{host}/index.php/api/client/chklogin
传入
username	账号
password	密码
client_key	客户端验证KEY
device_id	设备ID

返回
string	cid		账户ID
string	token
string	version	版本号
int		sertime	服务器时间
|--------------------------------------------------------------------------
*/	
	function chklogin()
	{
		//处理接收信息
		$username = trim($this->input->post('username'));
		$password = trim($this->input->post('password'));
		$client_key = trim($this->input->post('client_key'));
		$device_id = trim($this->input->post('device_id'));
		$needinfo  = intval($this->input->post('needinfo'));
		
		$status = $token = $version = $sertime = NULL;
		//提交资料不完整 返回-10001
		if(empty($username) || empty($password) || empty($client_key) || empty($device_id) ){
			$status = '-10001';	
			$info = array();
		}else{
			$status = $this->m_client->chklogin($username,$password,$client_key,$device_id);
			if($status > 0){
				//token = 用户ID_client_key_device_id
				$status = strval($status);
				$member_id = $status;
				//$token = md5(encrypt($status.'_'.$client_key.'_'.$device_id.'_'.uniqid());
				$token = md5($status.'_'.$client_key.'_'.$device_id.'_'.uniqid());
				$version = '0';
				$sertime = time();	
				//更新token
				$this->m_client->update_api_token($status,$device_id,$client_key,$token);
				
				
				//基本资料
				$info = array();
				if($needinfo=='1'){
					$info['memberinfo'] = $this->m_client->get_my_baseinfo($member_id); 
					if(!empty($info['memberinfo']['member_head'])){
						$info['memberinfo']['member_head'] = base_url().'pic/'.$info['memberinfo']['floder_pre'].'/'.$info['memberinfo']['floder'].'/'.'company/'.$info['memberinfo']['member_head'];	
					}
					
					$temp = $this->m_client->get_merchant_contact($member_id); 			
					$info['products'] = $this->m_client->merchant_product_list($member_id,0,100,0); 	
					$info['tag'] = $this->m_client->get_account_tag($member_id); 			
					
					if(!empty($temp)){
						$info['contact']['phone'] = $info['contact']['email'] = $info['contact']['address'] = $info['contact']['longitude'] = $info['contact']['latitude'] = $info['contact']['tel'] = '';
						foreach($temp as $line){
							if($line['type']=='4'){$info['contact']['phone'] = $line['content'];}
							if($line['type']=='2'){$info['contact']['email'] = $line['content'];}
							if($line['type']=='3'){$info['contact']['address'] = $line['content'];}
							if($line['type']=='5'){$info['contact']['tel'] = $line['content'];}
						}
					}else{
						$info['contact'] = array();
					}
				}
				
			}else{
				$info = array();	
			}
		}
		$res = array('status'=>$status,'token'=>$token,'version'=>$version,'sertime'=>$sertime,'info'=>$info);	
		echo json_encode($res);
		if($_REQUEST['debug']=='1'){debugit();}
	}
	
	


/*
|--------------------------------------------------------------------------
| 注册，返回token
{host}/index.php/api/client/reg

传入
username	账号
password	密码
client_key	客户端验证KEY
device_id	设备ID

返回
string	cid		账户ID
string	token
string	version	版本号
int		sertime	服务器时间
|--------------------------------------------------------------------------
*/	
	function reg()
	{
		$username = strtolower(trim($this->input->post('username')));
		$password = trim($this->input->post('password'));
		$client_key = trim($this->input->post('client_key'));
		$device_id = trim($this->input->post('device_id'));
		$key = trim($this->input->post('key'));		
		
		$status = $token = $version = $sertime = NULL;
		
		//提交资料不完整 返回-10001
		if(empty($username) || empty($password)|| empty($key) || empty($client_key) || empty($device_id) ){
			$status = '-10001';	
		}else{
			$chkkey = $this->m_client->chekey($key);
			//$chkkey = array();
			if(is_array($chkkey)){
				$res = $this->m_client->reg($username,$password,$client_key,$device_id,$key);
				//var_dump($res);die();
				if(is_array($res)){
					//token = 用户ID_client_key_device_id
					$status = strval($res['cid']);
					$key_status = $res['key_status'];
					$head_pic_static = $res['head_pic_static']; 
					//$token = encrypt($status.'_'.$client_key.'_'.$device_id);
					$token = md5($status.'_'.$client_key.'_'.$device_id.'_'.uniqid());
					$version = '0';
					$sertime = time();	
					$this->m_client->update_api_token($status,$device_id,$client_key,$token);
				}else{
					$status = $res;	
					$key_status = $head_pic_static = '0';
					
				}
			}else{
				$status = 	$chkkey;
			}
		}
		$info = array();
		$info['memberinfo'] = $info['contact'] = $info['products'] = $info['tag'] = array();
		$res = array('status'=>$status,'token'=>$token,'version'=>$version,'sertime'=>$sertime,'key_status'=>$key_status,'head_pic_static'=>$head_pic_static,'info'=>$info);	
		echo json_encode($res);
		if($_REQUEST['debug']=='1'){debugit();}
	}




/*
|--------------------------------------------------------------------------
| 返回各模块最新消息接口
{host}/index.php/api/client/last_news
|--------------------------------------------------------------------------
*/
	function last_news()
	{
		$lan = intval($this->input->post('lan'));
		$lan = empty($lan)?'1':$lan;
		
		//访问生成的json静态文件并返回
		$jsonfile = 'static/last_news_'.$lan.'.json';
		if(is_file($jsonfile)){
			echo file_get_contents($jsonfile); 
		}else{
			echo '-10004';	
		}
	}
	
/*
|--------------------------------------------------------------------------
| 消息列表接口：news_list
{host}/index.php/api/client/news_list
|--------------------------------------------------------------------------
*/
	function news_list()
	{
		$type_id = intval($this->input->post('type_id'));
		$lan = intval($this->input->post('lan'));
		$lan = empty($lan)?'1':$lan;
		$jsonfile = 'static/news_list_'.$type_id.'_'.$lan.'.json';	
		if(is_file($jsonfile)){
			echo file_get_contents($jsonfile); 
		}else{
			echo '-10004';	
		}
	}
	



/*
|--------------------------------------------------------------------------
| 根据token与ID返回好友列表
{host}/index.php/api/client/good_freind_ids
|--------------------------------------------------------------------------
*/	
	function good_freind_ids()
	{
		$member_id = intval($this->input->post('member_id'));
		$token = trim($this->input->post('token'));
		$client_key = trim($this->input->post('client_key'));
		$device_id = trim($this->input->post('device_id'));
		 
				
		$status = $version = $sertime = NULL;	
		//提交资料不完整 返回-10001
		if(empty($member_id) || empty($token) || empty($client_key) || empty($device_id) ){
			$status = '-10001';	
		}else{
			$chktoken = $this->m_client->chktoken($token,$client_key,$device_id);
			if(empty($chktoken)){
				$status = '-10003';	
			}else{			
				$info = array();
				$tempinfo = $this->m_client->good_freind_ids($member_id);
			
				if(!empty($tempinfo)){
					foreach($tempinfo as $line){
						$info[] = $line['toid']; 	
					}
					$status = '1';
				}else{
					$status = '0';
				}	
				
				$version = '0';
				$sertime = time();	
			}
		}
		
		$res = array('status'=>$status,'version'=>$version,'sertime'=>$sertime,'info'=>$info);	
		echo json_encode($res);
		
		if($_REQUEST['debug']=='1'){debugit();}

	}
	


/*
|--------------------------------------------------------------------------
| 根据token与ID 添加好友与解除好友接口
{host}/index.php/api/client/update_friend
|--------------------------------------------------------------------------
*/	
	function update_friend()
	{
		$member_id = intval($this->input->post('member_id'));
		$friend_id = intval($this->input->post('friend_id'));
		$operate = intval($this->input->post('operate'));
		$please_message = htmlspecialchars($this->input->post('please_message'));
		
		$longitude = trim($this->input->post('longitude'));
		$lantitude = trim($this->input->post('lantitude'));
		$request_type = intval($this->input->post('request_type')); //1 好友申请 2 打招呼
		
		$token = trim($this->input->post('token'));
		$client_key = trim($this->input->post('client_key'));
		$device_id = trim($this->input->post('device_id'));
		 
				
		$status = $version = $sertime = NULL;	
		//提交资料不完整 返回-10001
		if(empty($member_id) ||empty($friend_id) ||empty($operate) || empty($token) || empty($client_key) || empty($device_id) ){
			$status = '-10001';	
		}else{
			$chktoken = $this->m_client->chktoken($token,$client_key,$device_id);
			if(empty($chktoken)){
				$status = '-10003';	
			}else{			
				$status = $this->m_client->update_friend($member_id,$friend_id,$operate);
				if($status == '1'){
					$uname_a = $this->m_client->get_member_info($friend_id);
					$uname_b = $this->m_client->get_member_info($member_id);
					
					if(!empty($uname_a['member_head'])){
						$uname_a['member_head'] = base_url().'pic/'.$uname_a['floder_pre'].'/'.$uname_a['floder'].'/'.'company/'.$uname_a['member_head'];		
					}
					if(!empty($uname_b['member_head'])){
						$uname_b['member_head'] = base_url().'pic/'.$uname_b['floder_pre'].'/'.$uname_b['floder'].'/'.'company/'.$uname_b['member_head'];		
					}
					
					$message_a = array(
						'memberinfo'=>array('id'=>$friend_id,'floder'=>$uname_a['floder'],'floder_pre'=>$uname_a['floder_pre'],'u_name'=>$uname_a['u_name'],'member_head'=>$uname_a['member_head']),
						'pic_array'=>array(),
						'pronum'=>'',
						'message'=>'',
						'longitude'=>$longitude,
						'lantitude'=>$lantitude,
						'mess_time'=>time(),
						'type'=>$request_type,
						'operate'=>$operate
					);
					
					$message_b = array(
						'memberinfo'=>array('id'=>$member_id,'floder'=>$uname_b['floder'],'floder_pre'=>$uname_b['floder_pre'],'u_name'=>$uname_b['u_name'],'member_head'=>$uname_b['member_head']),
						'pic_array'=>array(),
						'pronum'=>'',
						'message'=>'',
						'longitude'=>$longitude,
						'lantitude'=>$lantitude,
						'mess_time'=>time(),
						'type'=>$request_type,
						'operate'=>$operate
					);
					
					$this->m_client->add_message($member_id,$friend_id,'1',json_encode($message_a),$uname_a['u_name'],$request_type);		
					$this->m_client->add_message($friend_id,$member_id,'1',json_encode($message_b),$uname_b['u_name'],$request_type);		
					
					
					
				}

				$sertime = time();	
			}
		}
		
		$res = array('status'=>$status,'sertime'=>$sertime);	
		echo json_encode($res);
		
		if($_REQUEST['debug']=='1'){debugit();}

	}



/*
|--------------------------------------------------------------------------
| 根据token 产品收藏
{host}/index.php/api/client/product_collect
|--------------------------------------------------------------------------
*/	
	function product_collect()
	{
		$member_id = intval($this->input->post('member_id'));
		$product_id = intval($this->input->post('product_id'));
		$operate = intval($this->input->post('operate'));
		
		$token = trim($this->input->post('token'));
		$client_key = trim($this->input->post('client_key'));
		$device_id = trim($this->input->post('device_id'));
		 
				
		$status = $version = $sertime = NULL;	
		//提交资料不完整 返回-10001
		if(empty($member_id) ||empty($product_id) ||empty($operate) || empty($token) || empty($client_key) || empty($device_id) ){
			$status = '-10001';	
		}else{
			$chktoken = $this->m_client->chktoken($token,$client_key,$device_id);
			if(empty($chktoken)){
				$status = '-10003';	
			}else{			
				$status = $this->m_client->product_collect($member_id,$product_id,$operate);
				if($status == '1'){		
					//更新产品收藏数量	
					$this->m_client->update_pro_collect_info($product_id,$operate);
					//更新消息
					//$uname = $this->m_client->get_account_name_frompro($product_id);	
					//$this->m_client->add_message($uname['account_id'],$member_id,'2','收藏了产品',$uname['u_name']);	
							
					if($operate == '1'){
						$userinfo = $this->m_client->get_account_name_frompro($product_id);
						$mess_memberinfo = $this->m_client->get_mess_memberinfo($member_id);
						$mess_memberinfo['mess_time'] = time();									
						$this->m_client->add_message($userinfo['account_id'],$member_id,'2',json_encode($mess_memberinfo),$userinfo['u_name'],'1');		
					}
				}
				$sertime = time();	
			}
		}
		
		$res = array('status'=>$status,'sertime'=>$sertime);	
		echo json_encode($res);
		
		if($_REQUEST['debug']=='1'){debugit();}

	}


/*
|--------------------------------------------------------------------------
| 根据token 获取会员收藏产品列表接口
{host}/index.php/api/client/get_collect_products
|--------------------------------------------------------------------------
*/	
	function get_collect_products()
	{
		$member_id = intval($this->input->post('member_id'));
		$page = intval($this->input->post('page'));
		$count_per_page = intval($this->input->post('count_per_page'));
		$search = trim($this->input->post('search'));
		
		$token = trim($this->input->post('token'));
		$client_key = trim($this->input->post('client_key'));
		$device_id = trim($this->input->post('device_id'));
		
		$count_per_page = empty($count_per_page)?'50':$count_per_page;
			
		$status = $version = $sertime = NULL;	
		//提交资料不完整 返回-10001
		if(empty($member_id)  || empty($token) || empty($client_key) || empty($device_id) ){
			$status = '-10001';	
		}else{
			$chktoken = $this->m_client->chktoken($token,$client_key,$device_id);
			if(empty($chktoken)){
				$status = '-10003';	
			}else{			
				$info = $this->m_client->get_collect_products($member_id,$page,$count_per_page,$search);
				if(is_array($info)){
					$status = '1'	;
				}else{
					$status = $info ;	
				}
				 
				$sertime = time();	
			}
		}
		
		$res = array('status'=>$status,'info'=>$info,'sertime'=>$sertime);	
		echo json_encode($res);
		
		if($_REQUEST['debug']=='1'){debugit();}

	}





/*
|--------------------------------------------------------------------------
| 根据token 评论产品
{host}/index.php/api/client/comment_product
|--------------------------------------------------------------------------
*/	
	function comment_product()
	{
		$member_id = intval($this->input->post('member_id'));
		$product_id = intval($this->input->post('product_id'));
		$comment = htmlspecialchars($this->input->post('comment'));
		
		$token = trim($this->input->post('token'));
		$client_key = trim($this->input->post('client_key'));
		$device_id = trim($this->input->post('device_id'));
		
		$status = $version = $sertime = NULL;	
		//提交资料不完整 返回-10001
		if(empty($member_id) || empty($product_id) || empty($comment) || empty($token) || empty($client_key) || empty($device_id) ){
			$status = '-10001';	
		}else{
			$chktoken = $this->m_client->chktoken($token,$client_key,$device_id);
			if(empty($chktoken)){
				$status = '-10003';	
			}else{			
				$status = $this->m_client->comment_product($member_id,$product_id,$comment); 
				if($status == '1'){			
					
					$userinfo = $this->m_client->get_account_name_frompro($product_id);
					$mess_memberinfo = $this->m_client->get_mess_memberinfo($member_id);
					$mess_memberinfo['mess_time'] = time();									
					$this->m_client->add_message($userinfo['account_id'],$member_id,'2',json_encode($mess_memberinfo),$userinfo['u_name'],'2');		
					
					
					//$uname = $this->m_client->get_account_name_frompro($product_id);	
					//$this->m_client->add_message($uname['account_id'],$member_id,'2','评论了产品',$uname['u_name']);	
					$status = '1';
				}
				$sertime = time();	
			}
		}
		
		$res = array('status'=>$status,'sertime'=>$sertime);	
		echo json_encode($res);
		
		if($_REQUEST['debug']=='1'){debugit();}

	}


/*
|--------------------------------------------------------------------------
| 根据token 获取产品评论列表接口
{host}/index.php/api/client/get_product_comments
|--------------------------------------------------------------------------
*/	
	function get_product_comments()
	{
		$product_id = intval($this->input->post('product_id'));
		$page = intval($this->input->post('page'));
		$count_per_page = intval($this->input->post('count_per_page'));

		
		$token = trim($this->input->post('token'));
		$client_key = trim($this->input->post('client_key'));
		$device_id = trim($this->input->post('device_id'));
		$debug = trim($this->input->post('debug'));
		
		$count_per_page = empty($count_per_page)?'50':$count_per_page;
		
		$status = $version = $sertime = NULL;	
		//提交资料不完整 返回-10001
		if(empty($product_id)  || empty($token) || empty($client_key) || empty($device_id) ){
			$status = '-10001';	
		}else{
			$chktoken = $this->m_client->chktoken($token,$client_key,$device_id);
			if(empty($chktoken)){
				$status = '-10003';	
			}else{			
				$info = $this->m_client->get_product_comments($product_id,$page,$count_per_page); 
				$status = '1';
				$sertime = time();	
			}
		}
		
		$res = array('status'=>$status,'sertime'=>$sertime,'info'=>$info);	
		echo json_encode($res);

		if($debug=='1'){debugit();}

	}


/*
|--------------------------------------------------------------------------
| 根据token 获取产品详情
{host}/index.php/api/client/get_product
|--------------------------------------------------------------------------
*/	
	function get_product()
	{
		$product_id = intval($this->input->post('product_id'));
	
		$token = trim($this->input->post('token'));
		$client_key = trim($this->input->post('client_key'));
		$device_id = trim($this->input->post('device_id'));
		$debug = trim($this->input->post('debug'));
		
		$count_per_page = empty($count_per_page)?'50':$count_per_page;
		
		$status = $version = $sertime = NULL;	
		//提交资料不完整 返回-10001
		//if(empty($product_id)  || empty($token) || empty($client_key) || empty($device_id) ){
		if(empty($product_id)  ){	
			$status = '-10001';	
		}else{
			//$chktoken = $this->m_client->chktoken($token,$client_key,$device_id);
			//if(empty($chktoken)){
				//$status = '-10003';	
			//}else{			
				$info = $this->m_client->get_product($product_id); 
				if(!empty($info)){
					$info['product_pics'] = $this->m_client->get_product_img($product_id); 
				}
				$status = '1';
				$sertime = time();	
			//}
		}
		
		$res = array('status'=>$status,'sertime'=>$sertime,'info'=>$info);	
		echo json_encode($res);

		if($debug=='1'){debugit();}

	}




/*
|--------------------------------------------------------------------------
| 根据token 商人产品接口
{host}/index.php/api/client/merchant_product_list
|--------------------------------------------------------------------------
*/	
	function merchant_product_list()
	{
		$member_id = intval($this->input->post('member_id'));
		$page = intval($this->input->post('page'));
		$count_per_page = intval($this->input->post('count_per_page'));
		$product_type_id = intval($this->input->post('product_type_id'));
		$account_id = intval($this->input->post('account_id'));
		
		$token = trim($this->input->post('token'));
		$client_key = trim($this->input->post('client_key'));
		$device_id = trim($this->input->post('device_id'));
		$debug = trim($this->input->post('debug'));
		
		$count_per_page = empty($count_per_page)?'50':$count_per_page;
		
		$status = $version = $sertime = NULL;	
		//提交资料不完整 返回-10001
		if(empty($member_id)  || empty($token) || empty($client_key) || empty($device_id) ){
			$status = '-10001';	
		}else{
			$chktoken = $this->m_client->chktoken($token,$client_key,$device_id);
			if(empty($chktoken)){
				$status = '-10003';	
			}else{			
				$info = $this->m_client->merchant_product_list($member_id,$page,$count_per_page,$product_type_id); 

				//产品收藏与产品列表结合
				if(!empty($account_id)){
					$collect_temp = $this->m_client->product_collect_list($account_id);
					if(!empty($collect_temp)){
						foreach($collect_temp as $collect_temp_line){
							$collect[] =  $collect_temp_line['product_id'];	
						}
					}
					
					if(!empty($collect)){
						foreach($info as $key=>$info_line){
							if(in_array($info_line['id'],$collect)){
								$info[$key]['collect']	= '1';
							}else{
								$info[$key]['collect']	= '0';
							}
						}
					}
				}else{
					foreach($info as $key=>$info_line){
						$info[$key]['collect']	= '0';
					}
				}
				
				
				$status = '1';
				$sertime = time();	
			}
		}
		$member_info = $this->m_client->get_member_info($member_id);
		$res = array('status'=>$status,'sertime'=>$sertime,'username'=>$member_info['username'],'info'=>$info);	
		echo json_encode($res);

		if($debug=='1'){debugit();}

	}
	
	
/*
|--------------------------------------------------------------------------
| 根据token 获取商人联系方式
{host}/index.php/api/client/get_merchant_contact
|--------------------------------------------------------------------------
*/	
	function get_merchant_contact()
	{
		$member_id = intval($this->input->post('member_id'));
	
		$token = trim($this->input->post('token'));
		$client_key = trim($this->input->post('client_key'));
		$device_id = trim($this->input->post('device_id'));
		
		$status = $version = $sertime = NULL;	
		//提交资料不完整 返回-10001
		if(empty($member_id)  || empty($token) || empty($client_key) || empty($device_id) ){
			$status = '-10001';	
		}else{
			$chktoken = $this->m_client->chktoken($token,$client_key,$device_id);
			if(empty($chktoken)){
				$status = '-10003';	
			}else{			
				$temp = $this->m_client->get_merchant_contact($member_id); 
				if(!empty($temp)){
					$info['phone'] = $info['email'] = $info['address'] = $info['tel'] = $info['longitude'] = $info['latitude'] = '';
					foreach($temp as $line){
						if($line['type']=='4'){$info['phone'] = $line['content'];}
						if($line['type']=='2'){$info['email'] = $line['content'];}
						if($line['type']=='3'){$info['address'] = $line['content'];}
						if($line['type']=='5'){$info['tel'] = $line['content'];}
					}
				}else{
					$info = array();
				}	
				$status = '1';
				$sertime = time();	
			}
		}
		
		$res = array('status'=>$status,'sertime'=>$sertime,'info'=>$info);	
		echo json_encode($res);

		if($_REQUEST['debug']=='1'){debugit();}

	}


/*
|--------------------------------------------------------------------------
| 根据token 我的中博会
{host}/index.php/api/client/get_my
|--------------------------------------------------------------------------
*/	

	function get_my()
	{
		$member_id = intval($this->input->post('member_id'));
	
		$token = trim($this->input->post('token'));
		$client_key = trim($this->input->post('client_key'));
		$device_id = trim($this->input->post('device_id'));
		
		$status = $version = $sertime = NULL;	
		//提交资料不完整 返回-10001
		if(empty($member_id)  || empty($token) || empty($client_key) || empty($device_id) ){
			$status = '-10001';	
		}else{
			$chktoken = $this->m_client->chktoken($token,$client_key,$device_id);
			if(empty($chktoken)){
				$status = '-10003';	
			}else{			
				$info = array();
				$info['memberinfo'] = $this->m_client->get_my_baseinfo($member_id); 
				if(!empty($info['memberinfo']['member_head'])){
					$info['memberinfo']['member_head'] = base_url().'pic/'.$info['memberinfo']['floder_pre'].'/'.$info['memberinfo']['floder'].'/'.'company/'.$info['memberinfo']['member_head'];	
				}
				
				$temp = $this->m_client->get_merchant_contact($member_id); 			
				$info['products'] = $this->m_client->merchant_product_list($member_id,0,100,0); 	
				$info['tag'] = $this->m_client->get_account_tag($member_id); 			
				
				if(!empty($temp)){
					$info['contact']['phone'] = $info['contact']['email'] = $info['contact']['address'] = $info['contact']['longitude'] = $info['contact']['latitude'] = $info['contact']['tel'] = '';
					foreach($temp as $line){
						if($line['type']=='4'){$info['contact']['phone'] = $line['content'];}
						if($line['type']=='2'){$info['contact']['email'] = $line['content'];}
						if($line['type']=='3'){$info['contact']['address'] = $line['content'];}
						if($line['type']=='5'){$info['contact']['tel'] = $line['content'];}
					}
				}else{
					$info['contact']['phone'] = $info['contact']['email'] = $info['contact']['address'] = $info['contact']['longitude'] = $info['contact']['latitude'] = $info['contact']['tel'] = '';
				}
				
				$status = '1';
				
			}
		}
		$sertime = time();	
		$res = array('status'=>$status,'sertime'=>$sertime,'info'=>$info);	
		echo json_encode($res);

		if($_REQUEST['debug']=='1'){debugit();}

	}


/*
|--------------------------------------------------------------------------
| 根据token 商人列表
{host}/index.php/api/client/member_list
|--------------------------------------------------------------------------
*/	

	function member_list()
	{
		$who = intval($this->input->post('who'));
		$page = intval($this->input->post('page'));
		$count_per_page = intval($this->input->post('count_per_page'));
		$account_id = intval($this->input->post('account_id'));
		$friend_only = intval($this->input->post('friend_only'));
		
		$token = trim($this->input->post('token'));
		$client_key = trim($this->input->post('client_key'));
		$device_id = trim($this->input->post('device_id'));
		
		$count_per_page = empty($count_per_page)?'50':$count_per_page;
		
		$status = $version = $sertime = NULL;	
		//提交资料不完整 返回-10001
		if(empty($token) || empty($client_key) || empty($device_id) ){
			$status = '-10001';	
		}else{
			$chktoken = $this->m_client->chktoken($token,$client_key,$device_id);
			if(empty($chktoken)){
				$status = '-10003';	
			}else{		
				
				$info = $this->m_client->member_list($who,$page,$count_per_page,$friend_only,$account_id); 
				if(empty($friend_only)){
					//好友列表与商人结合
					if(!empty($account_id)){
						$collect_temp = $this->m_client->good_freind_ids($account_id);
						if(!empty($collect_temp)){
							foreach($collect_temp as $collect_temp_line){
								$collect[] =  $collect_temp_line['toid'];	
							}
						}
						//print_r($collect);die();
						if(!empty($collect)){
							foreach($info as $key=>$info_line){
								if(in_array($info_line['account_id'],$collect)){
									$info[$key]['friend']	= '1';
								}else{
									$info[$key]['friend']	= '0';
								}
							}
						}else{
							foreach($info as $key=>$info_line){
								$info[$key]['friend']	= '0';
							}	
						}
					}else{
						foreach($info as $key=>$info_line){
							$info[$key]['friend']	= '0';
						}
					}
				}else{
					foreach($info as $key=>$info_line){
							$info[$key]['friend']	= '1';
						}	
				}
				
				
				$status = '1';
				$sertime = time();	
			}
		}
		$member_count = $this->m_client->member_list_num($who,$friend_only,$account_id);
		//$test = floor($member_count/$count_per_page);
		//echo $member_count;die();
		$res = array('status'=>$status,'sertime'=>$sertime,'page_count'=>ceil($member_count/$count_per_page),'member_count'=>$member_count,'info'=>$info);	
		echo json_encode($res);

		if($_REQUEST['debug']=='1'){debugit();}

	}



/*
|--------------------------------------------------------------------------
| 根据token 产品导航列表接口
{host}/index.php/api/client/product_list
|--------------------------------------------------------------------------
*/	

	function product_list()
	{
		$product_type_id = intval($this->input->post('product_type_id'));
		$page = intval($this->input->post('page'));
		$count_per_page = intval($this->input->post('count_per_page'));
		$account_id = intval($this->input->post('account_id'));
		$tag = trim($this->input->post('tag'));
		
		$token = trim($this->input->post('token'));
		$client_key = trim($this->input->post('client_key'));
		$device_id = trim($this->input->post('device_id'));
		
		$count_per_page = empty($count_per_page)?'50':$count_per_page;
		
		$status = $version = $sertime = NULL;	
		//提交资料不完整 返回-10001
		//if(empty($token) || empty($client_key) || empty($device_id) ){
		//	$status = '-10001';	
		//}else{
			//$chktoken = $this->m_client->chktoken($token,$client_key,$device_id);
			//if(empty($chktoken)){
				//$status = '-10003';	
			//}else{			
				$info = $this->m_client->product_list($product_type_id,$page,$count_per_page,$tag); 
				
				//产品收藏与产品列表结合
				if(!empty($account_id)){
					$collect_temp = $this->m_client->product_collect_list($account_id);
					if(!empty($collect_temp)){
						foreach($collect_temp as $collect_temp_line){
							$collect[] =  $collect_temp_line['product_id'];	
						}
					}
					//print_r($collect);die();
					if(!empty($collect)){
						foreach($info as $key=>$info_line){
							if(in_array($info_line['id'],$collect)){
								$info[$key]['collect']	= '1';
							}else{
								$info[$key]['collect']	= '0';
							}
						}
					}else{
						foreach($info as $key=>$info_line){
							$info[$key]['collect']	= '0';
						}	
					}
				}else{
					foreach($info as $key=>$info_line){
						$info[$key]['collect']	= '0';
					}
				}
				
				
				$status = '1';
				$sertime = time();	
			//}
		//}
		
		$res = array('status'=>$status,'sertime'=>$sertime,'info'=>$info);	
		echo json_encode($res);

		if($_REQUEST['debug']=='1'){debugit();}

	}






/*
|--------------------------------------------------------------------------
|  根据token添加新产品
{host}/index.php/api/client/add_new_product
|--------------------------------------------------------------------------
*/
	function add_new_product()
	{
		$member_id = intval($this->input->post('member_id'));
		$product_id = intval($this->input->post('product_id'));
		$product_name = htmlspecialchars($this->input->post('product_name'));
		$product_detail = htmlspecialchars($this->input->post('product_detail'));
		$tags = trim($this->input->post('tags'));
	
		$token = trim($this->input->post('token'));
		$client_key = trim($this->input->post('client_key'));
		$device_id = trim($this->input->post('device_id'));
	
		$status = $version = $sertime = NULL;
		//提交资料不完整 返回-10001
		//if(empty($member_id) || empty($product_name) || empty($product_detail) || empty($token) || empty($client_key) || empty($device_id) ){
		if(empty($member_id)  || empty($token)  || empty($product_name) || empty($client_key) || empty($device_id) ){
			$status = '-10001';
		}else{
			$chktoken = $this->m_client->chktoken($token,$client_key,$device_id);
			if(empty($chktoken)){
				$status = '-10003';
			}else{
				$status = $this->m_client->add_new_product($member_id,$product_id,$product_name,$product_detail,$tags);
				if($status){
					$pic_status = $this->m_client->upload_product_pic($member_id,$status);
				}
				//if($status){			
					//$uname = $this->m_client->get_account_name_frompro($product_id);	
					//$this->m_client->add_message($member_id,$member_id,'2','添加了新产品',$product_name);	
				//}
				
			}
		}
		$sertime = time();
		$res = array('status'=>$status,'sertime'=>$sertime,'pic_status'=>$pic_status);
		echo json_encode($res);
	
		if($_REQUEST['debug']=='1'){debugit();}
	
	}
	
	
/*
|--------------------------------------------------------------------------
|  上传产品图片
{host}/index.php/api/client/upload_product_pic
|--------------------------------------------------------------------------
*/
	function upload_product_pic()
	{
		$member_id = intval($this->input->post('member_id'));
		$product_id = intval($this->input->post('product_id'));
		$pic = $_POST['pic'];
		$token = trim($this->input->post('token'));
		$client_key = trim($this->input->post('client_key'));
		$device_id = trim($this->input->post('device_id'));
	
		$status = $version = $sertime = NULL;
		//提交资料不完整 返回-10001
		if(empty($member_id) || empty($product_id) || empty($token) || empty($client_key) || empty($device_id) ){
			$status = '-10001';
		}else{
			$chktoken = $this->m_client->chktoken($token,$client_key,$device_id);
			if(empty($chktoken)){
				$status = '-10003';
			}else{
				$status = $this->m_client->upload_product_pic($member_id,$product_id);
				$sertime = time();
			}
		}
		$res = array('status'=>$status,'sertime'=>$sertime);
		echo json_encode($res);
	
		if($_REQUEST['debug']=='1'){debugit();}
	
	}







/*
|--------------------------------------------------------------------------
|  获取消息
{host}/index.php/api/client/get_message_list
|--------------------------------------------------------------------------
*/
	function get_message_list()
	{
		
		$member_id = intval($this->input->post('member_id'));
		$page = intval($this->input->post('page'));
		$count_per_page = intval($this->input->post('count_per_page'));
		
		$token = trim($this->input->post('token'));
		$client_key = trim($this->input->post('client_key'));
		$device_id = trim($this->input->post('device_id'));
		
		$count_per_page = empty($count_per_page)?'50':$count_per_page;
		
		$status = $version = $sertime = NULL;	
		//提交资料不完整 返回-10001
		if(empty($token) || empty($client_key) || empty($device_id) ){
			$status = '-10001';	
		}else{
			$chktoken = $this->m_client->chktoken($token,$client_key,$device_id);
			if(empty($chktoken)){
				$status = '-10003';	
			}else{			
				$info = $this->m_client->get_message_list($member_id,$page,$count_per_page); 
				
				$status = '1';
				$sertime = time();	
			}
		}
		
		$res = array('status'=>$status,'sertime'=>$sertime,'info'=>$info);	
		echo json_encode($res);

		//if($_REQUEST['debug']=='1'){debugit();}

		
	}



/*
|--------------------------------------------------------------------------
| 根据token与企业账号新增修改企业标签
{host}/index.php/api/client/account_tag
|--------------------------------------------------------------------------
*/	
	function account_tag()
	{		
		$member_id = intval($this->input->post('member_id'));
		$tags = trim($this->input->post('tags'));
		$token = trim($this->input->post('token'));
		$client_key = trim($this->input->post('client_key'));
		$device_id = trim($this->input->post('device_id'));
		
		
		$status = $version = $sertime = NULL;	
		//提交资料不完整 返回-10001
		if(empty($member_id) ||empty($tags) ||empty($token) || empty($client_key) || empty($device_id) ){
			$status = '-10001';	
		}else{
			$chktoken = $this->m_client->chktoken($token,$client_key,$device_id);
			if(empty($chktoken)){
				$status = '-10003';	
			}else{			
				$status = $this->m_client->account_tag($member_id,$tags); 
				$sertime = time();	
			}
		}
		
		$res = array('status'=>$status,'sertime'=>$sertime);	
		echo json_encode($res);

		if($_REQUEST['debug']=='1'){debugit();}

		
	}	


/*
|--------------------------------------------------------------------------
| 根据token与企业账号返回企业信息
{host}/index.php/api/client/get_company_info
|--------------------------------------------------------------------------
*/		
	
	function get_company_info()
	{
		$companyuser = trim($this->input->post('companyuser'));
		$token = trim($this->input->post('token'));
		$client_key = trim($this->input->post('client_key'));
		$device_id = trim($this->input->post('device_id'));
		$lan = trim($this->input->post('lan'));
				
		$status = $version = $sertime = NULL;	
		//提交资料不完整 返回-10001
		if(empty($companyuser) || empty($token) || empty($client_key) || empty($device_id) ){
			$status = '-10001';	
		}else{
			$chktoken = $this->m_client->chktoken($token,$client_key,$device_id);
			if(empty($chktoken)){
				$status = '-10003';	
			}else{
				
				$info['companyinfo'] = array();
				$info['companyinfo'] = $this->m_client->get_company_info($companyuser);
				if(!empty($info['companyinfo']['company_id'])){
					$info['companyinfo']['contact'] = $this->m_client->get_company_contact($info['companyinfo']['company_id'],$lan);	
					if(!empty($info['companyinfo']['logo'])){
					$info['companyinfo']['logo'] = base_url().'pic/'.$companyuser.'/company/'.$info['companyinfo']['logo'];
				}
					if(!empty($info['companyinfo']['bgpic'])){
						$info['companyinfo']['bgpic'] = base_url().'pic/'.$companyuser.'/company/'.$info['companyinfo']['bgpic'];
					}
					$status = '1';
				}else{
					$info = array();
					$status = '-20002';	
				}
				
				$version = '0';
				$sertime = time();	
			}
		}
		
		$res = array('status'=>$status,'version'=>$version,'sertime'=>$sertime,'info'=>$info);	
		echo json_encode($res);
		
		if($_REQUEST['debug']=='1'){debugit();}
	}



/*
|--------------------------------------------------------------------------
| 根据token与企业账号返回企业产品所有信息
{host}/index.php/api/client/get_company_pro_list
|--------------------------------------------------------------------------
*/	
	
	function get_company_pro_list()
	{
		$companyuser = trim($this->input->post('companyuser'));
		$token = trim($this->input->post('token'));
		$client_key = trim($this->input->post('client_key'));
		$device_id = trim($this->input->post('device_id'));
		$lan = trim($this->input->post('lan'));
		
		$companyuser = 'test';
		$token = 'qs1ixjmPM8lVFn8EBq0nYeVpZsUEbuRSV';
		$client_key = '8kkll1Vx';
		$device_id = '1234567890';
		$lan = '1';
		
		$status = $version = $sertime = NULL;	
		//提交资料不完整 返回-10001
		if(empty($companyuser) || empty($token) || empty($client_key) || empty($device_id) ){
			$status = '-10001';	
		}else{
			$chktoken = $this->m_client->chktoken($token,$client_key,$device_id);
			if(empty($chktoken)){
				$status = '-10003';	
			}else{
				$info['product_list'] = array();
				$info['product_list'] = $this->m_client->get_company_pro_list($companyuser,$lan);
				
				
				if(!empty($info['product_list'])){
					$status = '1';
				}else{
					//$info = array();
					$status = '-20002';	
				}
				
				$version = '0';
				$sertime = time();	
			}
		}
		
		$res = array('status'=>$status,'version'=>$version,'sertime'=>$sertime,'info'=>$info);	
		echo json_encode($res);
		
		if($_REQUEST['debug']=='1'){debugit();}	
	}




	function testpost()
	{
//		$res1 = $this->input->post('res1');
//		$res2 = $this->input->get('res2');
//		$res3 = $_POST['res1'];
//		$res4 = $_REQUEST['res1'];
//		echo $res1.'<hr>'.$res2.'<hr>'.$res3.'<hr>'.$res4;
		echo 'post:<hr>';
		print_r($_POST);
		echo '<hr>get:<hr>';
		print_r($_GET);
		//debugit();
	}




/*
|--------------------------------------------------------------------------
| 返回最新tag
{host}/index.php/api/client/get_tags
|--------------------------------------------------------------------------
*/
	function get_tags()
	{
		//访问生成的json静态文件并返回
		$jsonfile = 'static/tags.json';
		if(is_file($jsonfile)){
			echo file_get_contents($jsonfile); 
		}else{
			echo '-10004';	
		}
	}




/*
|--------------------------------------------------------------------------
| 更新联系方式
{host}/index.php/api/client/update_contact
|--------------------------------------------------------------------------
*/
	function update_contact()
	{
		$member_id = intval($this->input->post('member_id'));
		$token = trim($this->input->post('token'));
		$client_key = trim($this->input->post('client_key'));
		$device_id = trim($this->input->post('device_id'));
		
		$u_name = trim($this->input->post('u_name'));
		$member_position = trim($this->input->post('member_position'));
		$company_name = trim($this->input->post('company_name'));
		
		$tags = trim($this->input->post('tags'));
		
		$tel = trim($this->input->post('tel'));
		$phone = trim($this->input->post('phone'));
		$email = trim($this->input->post('email'));
		$address = trim($this->input->post('address'));
		
		$status = $version = $sertime = NULL;	
		//提交资料不完整 返回-10001
		if(empty($member_id) ||empty($token) || empty($client_key) || empty($device_id) ){
			$status = '-10001';	
		}else{
			$chktoken = $this->m_client->chktoken($token,$client_key,$device_id);
			if(empty($chktoken)){
				$status = '-10003';	
			}else{			
				$this->m_client->update_contact($member_id,$u_name,$member_position,$company_name,$tel,$phone,$email,$address); 
				$tag = json_decode($tags);
				if(is_array($tag) && !empty($tag[0])){
					$this->m_client->account_tag($member_id,$tags);
				}
				$status = '1';
				$sertime = time();	
			}
		}
		
		$res = array('status'=>$status,'sertime'=>$sertime);	
		echo json_encode($res);

		if($_REQUEST['debug']=='1'){debugit();}
	}






/*
|--------------------------------------------------------------------------
|  上传头像
{host}/index.php/api/client/upload_headpic
|--------------------------------------------------------------------------
*/
	function upload_headpic()
	{
		$member_id = intval($this->input->post('member_id'));
		//$pic = $_POST['pic'];
		$token = trim($this->input->post('token'));
		$client_key = trim($this->input->post('client_key'));
		$device_id = trim($this->input->post('device_id'));
		//print_r($pic);die();
		$status = $version = $sertime = NULL;
		//提交资料不完整 返回-10001
		if(empty($member_id) || empty($token) || empty($client_key) || empty($device_id) ){
			$status = '-10001';
		}else{
			$chktoken = $this->m_client->chktoken($token,$client_key,$device_id);
			if(empty($chktoken)){
				$status = '-10003';
			}else{
				$status = $this->m_client->upload_headpic($member_id);
				$sertime = time();
			}
		}
		$res = array('status'=>$status,'sertime'=>$sertime);
		echo json_encode($res);
	
		if($_REQUEST['debug']=='1'){debugit();}
	
	}




/*
|--------------------------------------------------------------------------
|  收藏某个企业的所有产品
{host}/index.php/api/client/collect_company_products
|--------------------------------------------------------------------------
*/
	function collect_company_products()
	{
		$member_id = intval($this->input->post('member_id'));
		$company_id = intval($this->input->post('company_id'));
		
		$token = trim($this->input->post('token'));
		$client_key = trim($this->input->post('client_key'));
		$device_id = trim($this->input->post('device_id'));
		//print_r($pic);die();
		$status = $version = $sertime = NULL;
		//提交资料不完整 返回-10001
		if(empty($member_id) || empty($company_id) || empty($token) || empty($client_key) || empty($device_id) ){
			$status = '-10001';
		}else{
			$chktoken = $this->m_client->chktoken($token,$client_key,$device_id);
			if(empty($chktoken)){
				$status = '-10003';
			}else{
				$status = $this->m_client->collect_company_products($member_id,$company_id);
				$sertime = time();
			}
		}
		$res = array('status'=>$status,'sertime'=>$sertime);
		echo json_encode($res);
	
		if($_REQUEST['debug']=='1'){debugit();}
	
	}


/*
|--------------------------------------------------------------------------
|  发送消息,发送即时聊天
{host}/index.php/api/client/send_message
|--------------------------------------------------------------------------
*/
	function send_message()
	{
		$member_id = intval($this->input->post('member_id'));
		$from_member_id = intval($this->input->post('from_member_id'));
		//$type = intval($this->input->post('type'));
		$message = htmlspecialchars(addslashes($this->input->post('message')));
		$message_type = intval($this->input->post('message_type'));
		//$title = trim($this->input->post('title'));
		
		
		$token = trim($this->input->post('token'));
		$client_key = trim($this->input->post('client_key'));
		$device_id = trim($this->input->post('device_id'));

		$status = $version = $sertime = NULL;
		//提交资料不完整 返回-10001
		if(empty($member_id) || empty($message_type) || empty($token) || empty($client_key) || empty($device_id) ){
			$status = '-10001';
		}else{
			$chktoken = $this->m_client->chktoken($token,$client_key,$device_id);
			if(empty($chktoken)){
				$status = '-10003';
			}else{
				$status = '1';
				
				//上传文件（如果有）
				if($_FILES['userfile']['size']>0){
					$this->load->library('upload');
					$config['upload_path'] = 'mess_file/'.date('Ymd').'/';
					//$config['allowed_types'] = 'gif|jpg|png|amr|wav';
					$config['allowed_types'] = '*';
					$config['max_size'] = '50000';
					$config['max_width']  = '6400';
					$config['max_height']  = '6400';
					$config['encrypt_name']  = TRUE;
					
					if(!is_dir($config['upload_path'])){
						mkdir($config['upload_path'],0755,true)	;
					}
					
					$this->load->library('upload', $config);
					$this->upload->initialize($config);
					if (!$this->upload->do_upload())
					{
						$error = array('error' => $this->upload->display_errors());
						print_r($error);
						$status = '-60001';
					} else{
						$data = array('upload_data' => $this->upload->data());
						$message = base_url().$config['upload_path'].$data['upload_data']['file_name'];
						//print_r($this->upload->data());
						$status = '1';	
					}			 
				}else{
					if(empty($message)){
						$status = '-10001';	
					}
				}
				if($status == '1'){
					$message_id = $this->m_client->add_message($member_id,$from_member_id,'3',$message,'',$message_type);
					//$this->m_client->add_message($from_member_id,$member_id,'3',$message,'',$message_type);
				}
				$sertime = time();
			}
		}
		$res = array('status'=>$status,'sertime'=>$sertime,'message_id'=>$message_id);
		echo json_encode($res);
	
		if($_REQUEST['debug']=='1'){debugit();}	
	}


/*
|--------------------------------------------------------------------------
|  申请好友 type 1
{host}/index.php/api/client/friend_request
|--------------------------------------------------------------------------
*/
	function friend_request()
	{
		$member_id = intval($this->input->post('member_id'));
		$from_member_id = intval($this->input->post('from_member_id'));
		$message = trim($this->input->post('message'));
		$longitude = trim($this->input->post('longitude'));
		$lantitude = trim($this->input->post('lantitude'));
		$request_type = intval($this->input->post('request_type')); //1 好友申请 2 打招呼
		$reject = intval($this->input->post('reject')); //1 拒绝申请
			
		$token = trim($this->input->post('token'));
		$client_key = trim($this->input->post('client_key'));
		$device_id = trim($this->input->post('device_id'));


		$status = $version = $sertime = NULL;
		//提交资料不完整 返回-10001
		if(empty($member_id) || empty($from_member_id) || empty($token) || empty($client_key) || empty($device_id) ){
			$status = '-10001';
		}else{
			$chktoken = $this->m_client->chktoken($token,$client_key,$device_id);
			if(empty($chktoken)){
				$status = '-10003';
			}else{
				//检查是否发送过请求
				
				$chk = $this->m_client->chk_friend_request($member_id,$from_member_id,$request_type);
				
				if(!empty($chk) && empty($reject)){
					$status = '-30003';	
				}else{
				
					//发送方会员信息
					$mess_memberinfo = $this->m_client->get_mess_memberinfo($from_member_id,$reject);
					$mess_memberinfo['message'] = $reject?'拒绝了您的请求':$message;
					$mess_memberinfo['longitude'] = $longitude;
					$mess_memberinfo['lantitude'] = $lantitude;
					$mess_memberinfo['mess_time'] = time();
					$mess_memberinfo['type'] = $request_type;
					//print_r(json_encode($mess_memberinfo));die();
					$status = $this->m_client->add_message($member_id,$from_member_id,'1',json_encode($mess_memberinfo),'',$request_type);
					
				}
			}
		}
		$sertime = time();
		$res = array('status'=>strval($status),'sertime'=>$sertime);
		echo json_encode($res);
	 
		if($_REQUEST['debug']=='1'){debugit();}	
	}
	
	
	
/*
|--------------------------------------------------------------------------
|  消息轮询
{host}/index.php/api/client/get_message
|--------------------------------------------------------------------------
*/
	function get_message()
	{
		$member_id = intval($this->input->post('member_id'));
		$last_query = intval($this->input->post('last_query'));
		$count_per_page = intval($this->input->post('count_per_page'));		

		$token = trim($this->input->post('token'));
		$client_key = trim($this->input->post('client_key'));
		$device_id = trim($this->input->post('device_id'));

		$status = $version = $sertime = NULL;
		$count_per_page = empty($count_per_page)?'50':$count_per_page;
		
		//提交资料不完整 返回-10001
		$info = array();
		if(empty($member_id) || empty($token) || empty($client_key) || empty($device_id) ){
			$status = '-10001';
		}else{
			$chktoken = $this->m_client->chktoken($token,$client_key,$device_id);
			if(empty($chktoken)){
				$status = '-10003';
			}else{
				$status = '1';
				$infotemp = $this->m_client->get_message($member_id,$last_query,$count_per_page);
				
				$info['friend_mess'] = $info['pro_mess'] = $info['im_mess'] = $info['sys_mess'] = array();
				foreach($infotemp as $infoline){
					if($infoline['type'] != '3'){
						$infoline['message'] = json_decode($infoline['message']);
					}
					
					switch ($infoline['type'])	{
						case '1': $info['friend_mess'][] = $infoline;break;
						case '2': $info['pro_mess'][] = $infoline;break;
						//case '3': $info['im_mess'][] = $infoline;break;
						case '4': $info['sys_mess'][] = $infoline;break;
						default : $info['sys_mess'][] = $infoline;break;
					}
					
				}
				$info['im_mess'] = $this->m_client->get_im_message($member_id,$last_query,$count_per_page);
				$sertime = time();
			}
		}
		$res = array('status'=>$status,'sertime'=>$sertime,'info'=>$info);
		echo json_encode($res);
	
		//if($_REQUEST['debug']=='1'){debugit();}	
	}

	function get_im_message()
	{
		$member_id = intval($this->input->post('member_id'));
		$last_query = intval($this->input->post('last_query'));
		$count_per_page = intval($this->input->post('count_per_page'));		

		$token = trim($this->input->post('token'));
		$client_key = trim($this->input->post('client_key'));
		$device_id = trim($this->input->post('device_id'));

		$status = $version = $sertime = NULL;
		$count_per_page = empty($count_per_page)?'50':$count_per_page;
		
		//提交资料不完整 返回-10001
		$info = array();
		if(empty($member_id) || empty($token) || empty($client_key) || empty($device_id) ){
			$status = '-10001';
		}else{
			$chktoken = $this->m_client->chktoken($token,$client_key,$device_id);
			if(empty($chktoken)){
				$status = '-10003';
			}else{
				$status = '1';
				$info['im_mess'] = $this->m_client->get_im_message($member_id,$last_query,$count_per_page);				
			}
		}
		$sertime = time();
		$res = array('status'=>$status,'sertime'=>$sertime,'info'=>$info);
		echo json_encode($res);
	
		//if($_REQUEST['debug']=='1'){debugit();}	
	}
	
	function get_list_message()
	{
		$member_id = intval($this->input->post('member_id'));
		$last_query = intval($this->input->post('last_query'));
		$count_per_page = intval($this->input->post('count_per_page'));		

		$token = trim($this->input->post('token'));
		$client_key = trim($this->input->post('client_key'));
		$device_id = trim($this->input->post('device_id'));

		$status = $version = $sertime = NULL;
		$count_per_page = empty($count_per_page)?'50':$count_per_page;
		
		//提交资料不完整 返回-10001
		$info = array();
		if(empty($member_id) || empty($token) || empty($client_key) || empty($device_id) ){
			$status = '-10001';
		}else{
			$chktoken = $this->m_client->chktoken($token,$client_key,$device_id);
			if(empty($chktoken)){
				$status = '-10003';
			}else{
				$status = '1';
				$info = $this->m_client->get_message($member_id,$last_query,$count_per_page);	
				foreach($info as $key=>$infoline){
					if($infoline['type'] != '3'){
						$info[$key]['message'] = json_decode($infoline['message']);
					}
				}
			}
		}
		$sertime = time();
		$res = array('status'=>$status,'sertime'=>$sertime,'info'=>$info);
		echo json_encode($res);
	
		//if($_REQUEST['debug']=='1'){debugit();}	
	}	
	


	
/*
|--------------------------------------------------------------------------
|  标识消息已读
{host}/index.php/api/client/set_message_read
|--------------------------------------------------------------------------
*/
	function set_message_read()
	{
		$member_id = intval($this->input->post('member_id'));
		$message_ids = trim($this->input->post('message_ids'));

		$token = trim($this->input->post('token'));
		$client_key = trim($this->input->post('client_key'));
		$device_id = trim($this->input->post('device_id'));

		$status = $version = $sertime = NULL;
		
		//提交资料不完整 返回-10001
		$info = array();
		if(empty($member_id) || empty($token) || empty($client_key) || empty($device_id) ){
			$status = '-10001';
		}else{
			$chktoken = $this->m_client->chktoken($token,$client_key,$device_id);
			if(empty($chktoken)){
				$status = '-10003';
			}else{
				$message_ids_array = json_decode($message_ids);	
				if(!is_array($message_ids_array)){
					$status = '-10001';		
				}else{
					$status = $this->m_client->set_message_read($member_id,$message_ids_array);
				}
			}
		}
		$sertime = time();
		$res = array('status'=>$status,'sertime'=>$sertime);
		echo json_encode($res);
	
		if($_REQUEST['debug']=='1'){debugit();}	
	}



	
/*
|--------------------------------------------------------------------------
|  根据会员ID获取头像与姓名
{host}/index.php/api/client/get_my_baseinfo
|--------------------------------------------------------------------------
*/
	
	function get_my_baseinfo()
	{
		$member_id = intval($this->input->post('member_id'));
	
		$token = trim($this->input->post('token'));
		$client_key = trim($this->input->post('client_key'));
		$device_id = trim($this->input->post('device_id'));

		$status = $version = $sertime = NULL;
		
		//提交资料不完整 返回-10001
		$info = array();
		if(empty($member_id) || empty($token) || empty($client_key) || empty($device_id) ){
			$status = '-10001';
		}else{
			$chktoken = $this->m_client->chktoken($token,$client_key,$device_id);
			if(empty($chktoken)){
				$status = '-10003';
			}else{
				$infotemp = $this->m_client->get_my_baseinfo($member_id);		
				if(!empty($infotemp['member_head'])){
					$info['member_head'] = base_url().'pic/'.$infotemp['floder_pre'].'/'.$infotemp['floder'].'/'.'company/'.$infotemp['member_head'];	
				}		
			}
		}
		$sertime = time();
		$res = array('status'=>$status,'sertime'=>$sertime,'info'=>$info);
		echo json_encode($res);
	
		if($_REQUEST['debug']=='1'){debugit();}	
	}



/*
|--------------------------------------------------------------------------
|  版本
{host}/index.php/api/client/get_version
|--------------------------------------------------------------------------
*/
	function get_version()
	{
		$device = trim($this->input->post('device'));	
		if($device == 'android'){
			$version = '1.0'; 	
			$downloadurl = 'http://hmu080125.chinaw3.com/ios_app/CBD/CBD.apk';
		}else{
			$version = '0.1'; 	
			$downloadurl = '';	
		}
		$sertime = time();
		$res = array('status'=>'1','sertime'=>$sertime,'version'=>$version,'device'=>$device,'downloadurl'=>$downloadurl);
		echo json_encode($res);
	
		if($_REQUEST['debug']=='1'){debugit();}	
	}




 



/*
|--------------------------------------------------------------------------
|  获取画册用户资料与4张产品图片
{host}/index.php/api/client/get_version
|--------------------------------------------------------------------------
*/
	function select_base_from_no4e()
	{
		$status = $version = $sertime = NULL;
		
		//提交资料不完整 返回-10001
		$info = array();
		
		$this->load->library('Snoopy');	
		$snoopy = new Snoopy();	
		
		$formvars["from"] = '10';
		$formvars["page"] = intval($this->input->post('page'));
		$formvars["per_num"] = intval($this->input->post('per_num'));

		//$url = 'http://localhost/suixing';
		$url = 'http://c.no4e.com';
		$action = $url.'/index.php/api/io/select_base_from_no4e';//表单提交地址
		
		$snoopy->submit($action,$formvars);//$formvars为提交的数组
		$result = $snoopy->results;  //获取表单提交后的 返回的结果
		
		echo $result;
	}



//	function upload_test()
//	{
//		$xmlstr= file_get_contents("php://input");
//		$filename='temp/'.time().'.jpg';
//		if(file_put_contents($filename,$xmlstr)){
//			echo 'success';
//		}else{
//			echo 'failed';
//		}	
//	}
//	
//	
//	function upload_test2()
//	{
//		 
//		  $config['upload_path'] = 'temp/';
//		  $config['allowed_types'] = '*';
//		  $config['max_size'] = '10000';
//		  $config['max_width']  = '10240';
//		  $config['max_height']  = '7680';
//		  
//		  $this->load->library('upload', $config);
//			$this->upload->initialize($config);
//		  if ( ! $this->upload->do_upload())
//		  {
//		   $error = array('error' => $this->upload->display_errors());
//		   print_r($error);
//		  
//		  } 
//		  else
//		  {
//		   $data = array('upload_data' => $this->upload->data());
//		   print_r($data);
//		  }
//		  
//
//	}


}

/* End of file client.php */
/* Location: ./application/controllers/client.php */